<?php
namespace PFT;

class AllDis {
	const TABEL_NAME = 'pft_all_dis';

	public static function debug() {

	}

	public static function openAllDis($sid) {
		$exists = self::getConfig($sid);
		if ($exists) {
			$update = array(
				'status'=> 1
			);
			$result = Db::update($update, self::TABEL_NAME, $exists['id']);
		} else {
			$default_config = array(
				'develop_per' => 10,
				'recommend_per' => 20,
				'return_per'=> 40,
				'platform_per' => 0,
				'type' => 0
			);
			$insert = array(
				'sid' => $sid, 
				'status' => 1,
				'config' => json_encode($default_config)
			);
			$result = Db::insert($insert, self::TABEL_NAME);
		}
		return (bool)$result;
	}

	/**
	 * [getConfig description]
	 * @param  [type] $sid [description]
	 * @return [type]      [description]
	 */
	public static function getConfig($sid) {
		$sql = "select * from ".self::TABEL_NAME." where sid=:sid limit 1";
		$config = Db::fetch($sql, array(':sid' => $sid));
		return $config;
	}

	/**
	 * [saveConfig description]
	 * @param  [type] $sid    [description]
	 * @param  [type] $config [description]
	 * @return [type]         [description]
	 */
	public static function saveConfig($sid, $config) {
		$config = json_encode($config);
		$exists = self::getConfig($sid);
		if ($exists) {
			$update = array(
				'config'=> $config
			);
			$result = Db::update($update, self::TABEL_NAME, $exists['id']);
		} else {
			$insert = array(
				'sid' => $sid, 
				'config' => $config
			);
			$result = Db::insert($insert, self::TABEL_NAME);
		}
		return (bool)$result;
	}


}